package com.ykdl.member.kid.fittool;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.ykdl.member.kid.R;
import com.ykdl.member.kid.adapters.NotifyAdapter;
import com.ykdl.member.kid.beans.FeedNotifyBean;
import com.ykdl.member.kid.widget.RefreshListView;
import com.ykdl.member.views.ArrayWheelAdapter;
import com.ykdl.member.views.NumericWheelAdapter;
import com.ykdl.member.views.WheelView;

public class FeedNotifyActivity extends BaseActivity implements OnClickListener, OnItemLongClickListener{

	private static final String TAG = FeedNotifyActivity.class.getSimpleName();
	private RefreshListView mListView;
	private NotifyAdapter mAdapter;
	private List<FeedNotifyBean> mList;
	private View mLayerAddPage;
	private WheelView mWheelView1, mWheelView2, mWheelView3;
	private View mBtnCorss, mBtnRight;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_feed_notify);
		initView();
		mList = getData();
		mAdapter = new NotifyAdapter(this, mList);
		mListView.setAdapter(mAdapter);
		mListView.setMoreButtoIsGon(true);
		mListView.setOnItemLongClickListener(this);
		mAdapter.notifyDataSetChanged();
	}
	
	private List<FeedNotifyBean> getData(){
		SharedPreferences perfer = getSharedPreferences("notifyList", Context.MODE_PRIVATE);
		String value = perfer.getString("notify", "");
		if (TextUtils.isEmpty(value)) {
			return new ArrayList<FeedNotifyBean>();
		} else {
			GsonBuilder builder = new GsonBuilder();
			Gson gson = builder.create();
			return gson.fromJson(value, new TypeToken<List<FeedNotifyBean>>(){}.getType());
		}
	}
	
	private void saveDate(List<FeedNotifyBean> list){
		String value;
		if (list == null || list.size() == 0) {
			return;
		} else {
			GsonBuilder builder = new GsonBuilder();
			Gson gson = builder.create();
			value = gson.toJson(list);
		}
		SharedPreferences perfer = getSharedPreferences("notifyList", Context.MODE_PRIVATE);
		Editor editor = perfer.edit();
		editor.putString("notify", value);
		editor.commit();
	}
	
	private void initView(){
		findViewById(R.id.btnBack).setOnClickListener(this);
		findViewById(R.id.btnAdd).setOnClickListener(this);
		mListView = (RefreshListView) findViewById(R.id.listView);
		
		mLayerAddPage = findViewById(R.id.layerAddPage);
		mBtnCorss = findViewById(R.id.btnCross);
		mBtnCorss.setOnClickListener(this);
		mBtnRight = findViewById(R.id.btnRight);
		mBtnRight.setOnClickListener(this);
		
		mWheelView1 = (WheelView) findViewById(R.id.wheelView1);
		String[] strs = {"上午", "下午"};
		mWheelView1.setAdapter(new ArrayWheelAdapter<String>(strs));
		mWheelView2 = (WheelView) findViewById(R.id.wheelView2);
		mWheelView2.setAdapter(new NumericWheelAdapter(1, 12));
		//mWheelView1.setLabel("小时");
		mWheelView3 = (WheelView) findViewById(R.id.wheelView3);
		mWheelView3.setAdapter(new NumericWheelAdapter(0, 59));
		//mWheelView2.setLabel("分钟");
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnBack:
			saveDate(mList);
			finish();
			break;
		case R.id.btnAdd:
			mLayerAddPage.setVisibility(View.VISIBLE);
			mListView.setEnabled(false);
			break;
		case R.id.btnCross:
			mLayerAddPage.setVisibility(View.GONE);
			mListView.setEnabled(true);
		case R.id.btnRight:
			mLayerAddPage.setVisibility(View.GONE);
			FeedNotifyBean fb = createData();
			mList.add(fb);
			mAdapter.notifyDataSetChanged();
			saveDate(mList);
		default:
			break;
		}
	}
	
	private FeedNotifyBean createData(){
		int item1 = mWheelView1.getCurrentItem();
		int item2 = mWheelView2.getCurrentItem();
		int item3 = mWheelView3.getCurrentItem();
		Calendar calendar = Calendar.getInstance();
		if (item1 == 0) {
			calendar.set(Calendar.HOUR_OF_DAY, item2+1);
			calendar.set(Calendar.MINUTE, item3);
		} else {
			int h = (item2+1)+12;
			calendar.set(Calendar.HOUR_OF_DAY, h==24? 0:h);
			calendar.set(Calendar.MINUTE, item3);
		}
		FeedNotifyBean fb = new FeedNotifyBean();
		fb.setTime(calendar.getTimeInMillis());
		fb.setNotify(true);
		return fb;
	}
	
	private static final int DIALOG_DELETE_DATA = 1;
	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case DIALOG_DELETE_DATA:
			Dialog dialog = new AlertDialog.Builder(this).setMessage("是否删除?").setPositiveButton("确定", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					if (mDelBean != null) {
						mList.remove(mDelBean);
						mAdapter.notifyDataSetChanged();
					}
				}
			}).setNegativeButton("取消", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.cancel();
				}
			}).create();
			return dialog;

		default:
			return super.onCreateDialog(id);
		}
	}

	FeedNotifyBean mDelBean;
	@Override
	public boolean onItemLongClick(AdapterView<?> parent, View view,
			int position, long id) {
		mDelBean = (FeedNotifyBean) mAdapter.getItem(position-1);
		showDialog(DIALOG_DELETE_DATA);
		return false;
	}
	

}
